﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace DEEBCG.ExternalLib.CompanyAPI
{
    public class Utils
    {
        /// <summary>
        /// SHA1 加密，返回大写字符串
        /// </summary>
        /// <param name="content">需要加密字符串</param>
        /// <returns>返回40位UTF8 大写</returns>
        public static string Sha1(string content)
        {
            byte[] StrRes = Encoding.GetEncoding("UTF-8").GetBytes(content);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();

        }

        /// <summary>
        /// java时间戳格式时间戳转为C#格式时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>        
        public static DateTime GetTime(long timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = new TimeSpan(timeStamp * 10000);
            return dtStart.Add(toNow);
        }

        /// <summary>
        /// C# DateTime时间格式转换为Java时间戳格式  
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long ConvertDateTime(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (long)(time - startTime).TotalMilliseconds;
        }
    }
}
